<?php

if (! function_exists('fileFullPath')) {
    function fileFullPath($path)
    {
        if (empty($path)) {
            return '';
        }

        return 'https://' . env('OSS_BUCKET') . '.' . env('OSS_ENDPOINT') . '/' . $path;
    }
}


/**
 * 获取sql语句
 * @return String
 */
function getSql()
{
    \Illuminate\Support\Facades\DB::listen(function ($sql) {
        ($sql);
        $singleSql = $sql->sql;
        if ($sql->bindings) {
            foreach ($sql->bindings as $replace) {
                $value = is_numeric($replace) ? $replace : "'" . $replace . "'";
                $singleSql = preg_replace('/\?/', $value, $singleSql, 1);
            }
            dump($singleSql);
        } else {
            dump($singleSql);
        }
    });

}

/**
 * 内部使用 返回错误
 *
 * @param string $errmsg
 * @param int $errcode
 * @return array
 */
function resultError($errmsg = 'error', $errcode = 400, $data = [])
{
    return ['errcode' => $errcode, 'errmsg' => $errmsg, 'data' => $data];
}

;

/**
 * 内部使用 返回成功
 *
 * @param string $msg
 * @param int $code
 * @return array
 */
function resultSuccess($data = [], $errmsg = 'success', $errcode = 0)
{
    return ['errcode' => $errcode, 'errmsg' => $errmsg, 'data' => $data];
}

/**
 * 获取数组的字段s
 * @param array $arr 需要处理的数据
 * @param string $field 需要提取的key
 * @param int $isString 是否输出逗号间隔字符串
 * @param int $filterEmpty 是否过滤空值
 * @return array|string
 */
function getArrayFields($arr = [], $field = '', $isString = 0, $filterEmpty = 0) {
    $filedArr = [];
    foreach($arr as $v) {
        if($filterEmpty) {
            if(!empty($v[$field])) {
                $filedArr[$v[$field]] = $v[$field];
            }
        }
        else if(isset($v[$field])) {
            $filedArr[$v[$field]] = $v[$field];
        }
    }
    if(!$filedArr) {
        return $isString ? '': [];
    }
    if($isString) {
        return rtrim(implode(',', $filedArr), ', ');
    }
    $resArr = [];
    foreach($filedArr as $v) {
        array_push($resArr, $v);
    }
    unset($filedArr);

    return $resArr;
}

/**
 * (通用)将array的val转换为key
 * @param array $arr
 * @param $keyName
 * @return array
 */
function shiftValToKey($arr = [], $keyName = '') {
    if(!$arr) {
        return $arr;
    }
    $arrRes = [];
    if(!$keyName) {
        foreach($arr as $val) {
            $arrRes[$val] = $val;
        }
        unset($val);
        return $arrRes;
    }
    foreach($arr as $val) {
        $arrRes[$val[$keyName]] = $val;
    }
    unset($val);

    return $arrRes;
}
/**
 * (通用)将array的每个val转换key,数组，key分组
 * @param array $arr
 * @param $keyName
 * @return array
 */
function shiftValToKeyGroup($arr = [], $keyName) {
    $arrRes = [];
    foreach($arr as $val) {
        $arrRes[$val[$keyName]][] = $val;
    }
    unset($val);

    return $arrRes;
}


/**
 * 获取随机数字（验证码）
 * @param int $number
 * @return string
 */
if (!function_exists('getRandomCode')) {
    function getRandomCode ($number = 6) {
        $randomNumbers = [];
        for ($i = 0; $i < $number; $i++) {
            $randomNumbers[] = rand(0, 9);
        }
        return implode('', $randomNumbers);
    }
}


/**
 * 隐藏数字，手机号，身份证号等
 * @param $number
 * @param int $prefixNum    前面显示多少个字符
 * @param int $suffixNum    后面显示多少个字符
 * @return string
 */
if (!function_exists('hideNumber')) {
    function hideNumber ($number, $prefixNum = 3, $suffixNum = 4) {
        $prefix = substr($number, 0, $prefixNum);
        $suffix = substr($number, - $suffixNum);
        $replaceCount = strlen($number) - $prefixNum - $suffixNum;
        $replacement = str_repeat("*", $replaceCount);
        $newString = $prefix . $replacement . $suffix;
        return $newString;
    }
}

/**
 * 是否开市
 */
if (!function_exists('isOpening')) {
    function isOpening ($date = null, $time = null) {
        $date = empty($date) ? date('y-m-d') : $date;
        $time = empty($time) ? date('H:i:s') : $time;
        // 先判断时间是否在范围内
        if (!($time > '09:30' && $time <= '11:30') || $time > '13:00' && $time <= '15:00') {
            return false;
        }
        // 再判断是否节假日
        $isOpen = App\Models\DateModel::where('date', $date)
            ->value('is_open');
        if ($isOpen !== App\Models\DateModel::IS_OPEN) {
            return false;
        }
        return true;
    }
}

/**
 * 获取随机字符串
 */
if (!function_exists('getRandomString')) {
    function getRandomString($length = 10) {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $randString = '';
        for ($i = 0; $i < $length; $i++) {
            $randString .= $chars[rand(0, strlen($chars) - 1)];
        }
        return $randString;
    }
}

/**
 * 密码加密
 */
if (!function_exists('getEncryptPassword')) {
    function getEncryptPassword($password, $salt) {
        return md5($password.$salt);
    }
}
